//
//  ServiceView.swift
//  TianTianBiTing
//
//  Created by Gox mac 2 on 2019/3/20.
//  Copyright © 2019 Gox_WallyDiao. All rights reserved.
//

import UIKit

class ServiceView: UIView {
    
    var imageView:UIImageView?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.backgroundColor = UIColor.clear
        setUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setUI() {
        self.backgroundColor = RGB_Same(0, 0.8)
        
        let button = UIButton(type: UIButton.ButtonType.custom)
        button.frame = FRAME_CGFloat(SCREEN_WIDTH - 45, 25, 20, 20)
        button.setImage(IMAGE(name: "signRed_close"), for: .normal)
        button.addTarget(self, action: #selector(closeBtnAction), for: .touchUpInside)
        self.addSubview(button)
        
        imageView = UIImageView.init(frame: FRAME_CGFloat(SCREEN_WIDTH / 2 - 161.5, 64, 323, 346))
        self.addSubview(imageView!)
        imageView!.isUserInteractionEnabled = true
        self.addSubview(imageView!)
        let tap = UILongPressGestureRecognizer(target: self, action: #selector(longTapAction(longPressGesture:)))
        tap.minimumPressDuration = 1
        imageView?.addGestureRecognizer(tap)
    }
    
    @objc func closeBtnAction() {
        self.removeFromSuperview()
        
    }
    
    @objc func longTapAction(longPressGesture:UIGestureRecognizer) {
        if longPressGesture.state == .began {
            UIImageWriteToSavedPhotosAlbum((imageView?.image)!, self, #selector(image(image:didFinishSavingWithError:contextInfo:)), nil)
        }
    }
    
    @objc func image(image: UIImage, didFinishSavingWithError: NSError?, contextInfo: AnyObject) {

        if didFinishSavingWithError != nil {
            TTAlertView.showMessage(message: "保存失败，请从新尝试")
            return
        }
        TTAlertView.showMessage(message: "已保存到相册", second: 2.5)
        self.removeFromSuperview()
    }

}
